use log::LevelFilter;
use sqlx::ConnectOptions;
use sqlx::mysql::{MySqlConnectOptions, MySqlPoolOptions};
use crate::extends::core::config::framework::Datasource;

#[derive(Debug)]
pub struct DatabaseManager;

impl DatabaseManager {
    /// Initialize the connection pool for MySQL database
    pub async fn init_mysql_pool(config: &Datasource) -> sqlx::Result<sqlx::MySqlPool> {
        let connect_options = MySqlConnectOptions::new()
            .host(&config.host)
            .port(config.port)
            .username(&config.username)
            .password(&config.password)
            .database(&config.database)
            .log_statements(LevelFilter::Info)
            .clone();

        MySqlPoolOptions::new()
            .max_connections(config.db_pool.pool_size as u32)
            .connect_with(connect_options)
            .await
    }
}
